package dfsketches.gr3d;

import java.util.HashMap;

public class CommonAnimation implements Animation {

	public CommonAnimation(HashMap<Bone, Spline> splines) {
		boneAnim = splines;
		duration = calcDuration();
	}
	
	@Override
	public Matrix3D getTransformation(Bone bone, float time) {
		Spline spl;
		if ((spl = boneAnim.get(bone)) != null) {
			return spl.getTransformation(time);
		} else {
			return Matrix3D.getUniteMatrix();
		}
	}

	@Override
	public Matrix3D getTransformation(Bone bone, float time, Matrix3D output) {
		Spline spl;
		if ((spl = boneAnim.get(bone)) != null) {
			return spl.getTransformation(time, output);
		} else {
			return output.makeUnite();
		}
	}

	@Override
	public float getDuration() {
		return duration;
	}
	
	private float calcDuration() {
		float maxDuration = 0;
		for (Spline spline : boneAnim.values()) {
			maxDuration = Math.max(spline.getEndTime(), maxDuration);
		}
		return maxDuration;
	}
	
	private HashMap<Bone, Spline> boneAnim;
	private float duration;

}